<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>京东首页特效</title>
    <link rel="stylesheet" href="../css/common.css">
    <style>
        #box {
            height: 500vh;
        }

        #box>div {
            width: 100vw;
            height: 100vh;
            text-align: center;
            font-size: 50px;
        }

        #nav {
            width: 50px;
            border: 1px solid #cccccc;
            position: fixed;
            right: 50px;
            top: 200px;
        }

        #nav li {
            width: 50px;
            height: 50px;
            background: #cccccc;
            color: white;
            margin: 5px 0px;
            text-align: center;
            line-height: 50px;
        }
        #nav li.active{
            background: orangered;
        }

        .top {
            width: 100vw;
            height: 200vh;
            text-align: center;
            font-size: 50px;
            background-color: lightyellow;
        }

        .one {
            background-color: lightgreen;
        }

        .two {
            background-color: lightpink;
        }

        .three {
            background-color: lightskyblue;
        }

        .four {
            background-color: lightseagreen;
        }

        .five {
            background-color: lightsalmon;
        }
    </style>
</head>

<body>
    <div id="search"></div>
    <div id="nav">
        <ul>
            <li>one</li>
            <li>two</li>
            <li>three</li>
            <li>four</li>
            <li>five</li>
        </ul>
    </div>
    <div class="top"></div>
    <div id="box">
        <div class="one">one</div>
        <div class="two">two</div>
        <div class="three">three</div>
        <div class="four">four</div>
        <div class="five">five</div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script>
        $(window).scroll(function () {
            fun()
        })

        function fun() {
            //屏幕高度
            let wtop = $(window).innerHeight()
            //滚动高度
            let stop = $(window).scrollTop()
            $('#box>div').each(function (index, item) {
                let leave = wtop+stop-$(item).offset().top
                if(leave>0 && leave<wtop){
                    $("#nav li").eq(index).addClass('active').siblings('.active').removeClass('active')
                }
                if(index===0){
                    if(leave<0 || leave>wtop){
                        $("#nav li").removeClass('active')
                    }
                }
            })
        }
    </script>
</body>

</html>